package com.hospital.controller;

import com.alibaba.fastjson.JSONObject;
import com.hospital.entity.Check;
import com.hospital.entity.Employee;
import com.hospital.mapper.CheckMapper;
import com.hospital.tools.JsonResult;
import com.hospital.tools.MD5Utils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Map;

@RestController
@RequestMapping("/check")
public class CheckController {
    @Resource
    CheckMapper checkMapper;

    @GetMapping("/get_all")
    public JsonResult getAllCheck() {return JsonResult.json_ok(checkMapper.getAllCheck());}

    // 通过 Cid 获取数据
    @PostMapping("/get_by_id")
    public JsonResult getCheckByPatientId(
            @Valid @NotNull(message = "检查单ID不得为空") @RequestBody String params,
            BindingResult bindingResult
    ) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            // 100 为参数错误
            return JsonResult.json_error("100", error.getDefaultMessage());
        }
        JSONObject jsonObject = JSONObject.parseObject(params);
        Integer cid = jsonObject.getInteger("cid");
        if (cid == null) {
            return JsonResult.json_error("100", "检查单ID不可为空");
        }
        Check c = checkMapper.getCheckByPatientId(cid);
        if (c == null) {
            return JsonResult.json_error("303", "该检查单不存在");
        }
        return JsonResult.json_ok(c);
    }

    @PostMapping("/get_by_patient_id")
    public JsonResult getEmployeeByPhone(
            @Valid @NotNull(message = "病人ID不得为空") @RequestBody String params,
            BindingResult bindingResult
    ) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            // 100 为参数错误
            return JsonResult.json_error("100", error.getDefaultMessage());
        }

        JSONObject jsonObject = JSONObject.parseObject(params);
        Integer pid = jsonObject.getInteger("pid");
        if (pid == null) {
            return JsonResult.json_error("100", "病人id不可为空");
        }
        Check c = checkMapper.getCheckByPatientId(pid);
        if (c == null) {
            return JsonResult.json_error("303", "该病人没有检查单");
        }
        return JsonResult.json_ok(c);
    }

    // 添加医院员工接口
    @PostMapping("/add")
    public JsonResult addCheck(
            @Valid @RequestBody Check check,
            BindingResult bindingResult
    ) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            // 100 为参数错误
            return JsonResult.json_error("100", error.getDefaultMessage());
        }

        try {
            int res = checkMapper.insertCheck(check);
            return res == 1 ?
                    JsonResult.json_ok("添加成功") :
                    JsonResult.json_error("401", "添加失败，原因未知");
        } catch (DuplicateKeyException e) {
            return JsonResult.json_error("400", "检查单已存在");
        }

    }
    //更新检查单
    @PostMapping("/update")
    public JsonResult updateCheck(
            @Valid @RequestBody Check check,
            BindingResult bindingResult
    ) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            // 100 为参数错误
            return JsonResult.json_error("100", error.getDefaultMessage());
        }

        try {
            int res = checkMapper.updateCheck(check);
            return res == 1 ?
                    JsonResult.json_ok("修改成功") :
                    JsonResult.json_error("401", "修改失败，原因未知");
        } catch (DuplicateKeyException e) {
            return JsonResult.json_error("400", "检查单号冲突");
        } catch (Exception e) {
            return JsonResult.json_error("400", e.getMessage());
        }
    }


}
